package com.example.keshe0337.ui.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Contacts;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.example.keshe0337.R;
import com.example.keshe0337.adapter.ParkingRecordAdapter;
import com.example.keshe0337.adapter.RentAdapter;
import com.example.keshe0337.biz.RecordService;
import com.example.keshe0337.biz.RentService;
import com.example.keshe0337.entity.ParkingRecord;
import com.example.keshe0337.entity.ParkingSpace;
import com.example.keshe0337.utils.TimeConverters;

import java.util.List;

public class OderFragment extends Fragment {
    ListView listView;
    Context context;
    RecordService recordService;
    ParkingRecordAdapter parkingRecordAdapter;
    Long UID;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view0 = inflater.inflate(R.layout.fragment_all_parking, container, false);
        listView = view0.findViewById(R.id.listView);  // 用 view 来查找 ListView
        context = getContext();
        if (context == null) {
            Log.d("TEXT","OderFragment的context为空");
            return view0;
        }
        ImageButton button1 = view0.findViewById(R.id.addButton);
        ImageButton button2 = view0.findViewById(R.id.quitButton);
        button1.setVisibility(View.GONE);
        button2.setVisibility(View.GONE);
        SharedPreferences prefs = context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
        UID = prefs.getLong("UID", 1);
        recordService = new RecordService(context);
        List<ParkingRecord> userPRs = recordService.getPRsByUid(UID);
        for (ParkingRecord p : userPRs) {
            Log.d("TEXT", String.valueOf(p.getSpaceNum()));
        }

        parkingRecordAdapter = new ParkingRecordAdapter(context,userPRs);
        listView.setAdapter(parkingRecordAdapter);

        listView.setOnItemClickListener((parent, view, position, id) -> {
            ParkingRecord parkingRecord = parkingRecordAdapter.getItem(position);
            if (parkingRecord != null) {
             //Log.d("PR DISPLAY","管理员点击记录："+parkingRecord.getId());
             showPRDisplayDialog(parkingRecord);
             Log.d("PR DISPLAY","用户点击记录："+parkingRecord.getId());
            }
        });

        listView.setOnItemLongClickListener((parent, view, position, id) -> {
            ParkingRecord parkingRecord = parkingRecordAdapter.getItem(position);
            new AlertDialog.Builder(context)
                    .setTitle("确认删除")
                    .setMessage("是否确认删除该记录？")
                    .setPositiveButton("确认", (dialog, which) -> {
                        recordService.deletePRById(parkingRecord.getId());
                        parkingRecordAdapter.update(recordService.getPRsByUid(UID));
                        Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                    })
                    .setNegativeButton("取消", (dialog, which) -> {
                    })// 用户点击取消则关闭会话
                    .show();
            return true; // 表示事件已处理
        });


        return view0;

    }

    TextView parkingNumText, vehiclePlateText, startTimeText, endTimeText, totalTimeText,feeText;
    private void showPRDisplayDialog(ParkingRecord parkingRecord){
        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_pr_display_user, null);

        parkingNumText = dialogView.findViewById(R.id.textParkingNumber);
        vehiclePlateText = dialogView.findViewById(R.id.textVehiclePlate);
        startTimeText = dialogView.findViewById(R.id.textStartTime);
        endTimeText = dialogView.findViewById(R.id.textEndTime);
        totalTimeText = dialogView.findViewById(R.id.textTotalTime);
        feeText = dialogView.findViewById(R.id.textFee);

        parkingNumText.setText(parkingRecord.getSpaceNum());
        vehiclePlateText.setText(parkingRecord.getVehiclePlate());
        startTimeText.setText(TimeConverters.format(parkingRecord.getStartTime()));
        endTimeText.setText(TimeConverters.format(parkingRecord.getEndTime()));
        totalTimeText.setText(parkingRecord.getTotalTime()+"小时");
        feeText.setText(parkingRecord.getFee()+"￥");

        AlertDialog dialogHolder = new AlertDialog.Builder(context)
                .setView(dialogView)
                .create();

        dialogHolder.show();
    }
}
